#include "Main.h"

Vars_t Vars;
DWORD dwRecentScriptId = NULL;
CArrayEx<ScriptData*, ScriptData*> Scripts;

BOOL WINAPI DllMain(HINSTANCE hDll,DWORD dwReason,LPVOID lpReserved)
{
	if(dwReason == DLL_PROCESS_ATTACH)
	{
		Vars.hDLL = hDll;
		GetModuleFileName(hDll,Vars.szPath,MAX_PATH);
		PathRemoveFileSpecA(Vars.szPath);
		strcat(Vars.szPath,"\\");

		Scripts.RemoveAll();

		InitializeCriticalSection(&Vars.cCmdUnitSection);
		InitializeCriticalSection(&Vars.cScriptSection);

		Vars.lpW3TLSEntry = NULL;

		DefineOffsets();
		InstallPatchs();
	}
	else if(dwReason == DLL_PROCESS_DETACH)
	{
		RemovePatchs();

		for(ScriptData* pScript = FindScript(NULL, NULL); pScript; pScript = FindNextScript(pScript->GetScriptId(), NULL))
			TerminateThread(pScript->GetScriptThreadHandle(), NULL);
	}

	return TRUE;
}